﻿//-----------------------------------------------------------------------
// <copyright file="WindowStyleHelper.cs"">
//
//   *** IMPORTANT - PLEASE READ ***
//
//   The MIT License (MIT)
//
//   Copyright (c) 2011 NothingButNet
//
//   Permission is hereby granted, free of charge, to any person 
//   obtaining a copy of this software and associated documentation files 
//   (the "Software"), to deal in the Software without restriction, 
//   including without limitation the rights to use, copy, modify, merge,
//   publish, distribute, sublicense, and/or sell copies of the Software,
//   and to permit persons to whom the Software is furnished to do so, 
//   subject to the following conditions:
//
//   The above copyright notice and this permission notice shall be 
//   included in all copies or substantial portions of the Software.
//
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
//   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
//   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
//   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
//   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
//   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
//   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// </copyright>
//-----------------------------------------------------------------------

namespace Glass.Interop
{
    using System;
    using System.Diagnostics;
    using System.Runtime.InteropServices;

    /// <summary>
    /// Various interop methods.
    /// </summary>
    public static class WindowStyleHelper
    {
        #region Fields

        /// <summary>
        /// Used to set a new extended window style.
        /// </summary>
        /// <remarks>
        /// For more information, refer to http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx.
        /// </remarks>
        private const int GWL_EXSTYLE = -20;

        /// <summary>
        /// The window style layered extension flag.
        /// </summary>
        private const int WS_EX_LAYERED = 0x80000;

        /// <summary>
        /// The alpha transparency flag.
        /// </summary>
        private const int LWA_ALPHA = 0x02;

        #endregion // Fields

        #region Methods

        public static bool SetTransparency(Process process, byte transparency)
        {
            int windowLong =
                WindowStyleHelper.GetWindowLong(
                    process.MainWindowHandle,
                    WindowStyleHelper.GWL_EXSTYLE);

            WindowStyleHelper.SetWindowLong(
                 process.MainWindowHandle,
                 WindowStyleHelper.GWL_EXSTYLE,
                 windowLong | WindowStyleHelper.WS_EX_LAYERED);

            return
                WindowStyleHelper.SetLayeredWindowAttributes(
                    process.MainWindowHandle,
                    0,
                    transparency,
                    WindowStyleHelper.LWA_ALPHA);
        }

        /// <summary>
        /// Gets the window property.
        /// </summary>
        /// <param name="hWnd">The window handle.</param>
        /// <param name="nIndex">The window property index.</param>
        /// <returns>A flag indicating the result of the operation.</returns>
        /// <remarks>
        /// For more information, refer to http://msdn.microsoft.com/en-us/library/ms633584(VS.85).aspx.
        /// </remarks>
        [DllImport("user32.dll")]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

        /// <summary>
        /// Sets the window property.
        /// </summary>
        /// <param name="hWnd">The window handle.</param>
        /// <param name="nIndex">The window property.</param>
        /// <param name="dwNewLong">The dw new long.</param>
        /// <returns>A flag indicating the result of the operation.</returns>
        /// <remarks>
        /// For more information, refer to http://msdn.microsoft.com/en-us/library/windows/desktop/ms633591(v=vs.85).aspx.
        /// </remarks>
        [DllImport("user32.dll")]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        /// <summary>
        /// Sets the layered window attributes.
        /// </summary>
        /// <param name="hWnd">The window handle.</param>
        /// <param name="crKey">The cr key.</param>
        /// <param name="bAlpha">The alpha transparency level between 0 and 255 inclusive.</param>
        /// <param name="dwFlags">The dw flags.</param>
        /// <returns><c>true</c> if the operation succeeds; otherwise, <c>false</c>.</returns>
        /// <remarks>
        /// For more information, refer to http://msdn.microsoft.com/en-us/library/ms633540(VS.85).aspx.
        /// </remarks>
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, int bAlpha, uint dwFlags);

        #endregion // Methods
    }
}